<?php
/** ***********************
 * 作者：Zpy 
 * 日期：2016/11/29
 * 作用：渠道中心视图模型
 ** ***********************/
class centerViewHome extends coreFrameworkView
{
	public $tickket_class;
	public $post;
	public $channel_id;
	function __construct(){
		parent::__construct();
	    $this->channel_id=$this->GVar->session['user_info']['channel_id'];
		if (!$this->GVar->session['user_info']){
			MessageClass::ShowMessageComponent(_lang_login_no,'/core/login.html',true,'','',3);
			die;
		}
		
			
	}
	/*---------------zpy------------------*/
	function index(){
		$this->tmp->assign('applist',$this->channel_id);
		$this->tmp->assign("main_show_block","main.html");
		$this->dp('home/com_center/index');
	}
	//待开票
	function readyTicket(){
		
		$id = $this->channel_id;
        $id=3;
		$shop_data = $this->model->getBusShopByChannelIdNoPage($id);
		foreach ($shop_data as $v){
			$shop_id[] = $v['id'];
			$shop_name[$v['id']] = $v['business_name'];
		}
		
		$where[] = 'status=1';
		$where[] = 'shop_id in('.implode(',',$shop_id).')';
		$datas = $this->model->getTicketList($where);
		
		foreach ($datas as $k=>$v){
			$datas[$k]['buiness_name'] = $shop_name[$v['shop_id']];
		}
		$this->tmp->assign('datas',$datas);
		
		
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","ticket_list.html");
		$this->dp('home/com_center/index');
		
	}
	//已开票
	function isuseTicket(){
		$id = $this->channel_id;
        $id=3;
		$shop_data = $this->model->getBusShopByChannelId($id);
		
		
		foreach ($shop_data as $v){
			$shop_id[] = $v['id'];
			$shop_name[$v['id']] = $v['business_name'];
		}
		
		$where[] = 'status=2';
		$where[] = 'shop_id in('.implode(',',$shop_id).')';
		$datas = $this->model->getTicketList($where);
		
		foreach ($datas as $k=>$v){
			$datas[$k]['buiness_name'] = $shop_name[$v['shop_id']];
		}
//		print_r('<pre>');
//		print_r($datas);
//		print_r('</pre>');
		$this->tmp->assign('datas',$datas);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","ticket_list.html");
		$this->dp('home/com_center/index');
	}

	/*---------------zpy------------------*/
	//已冲红
	function isuseRed(){
		$id = $this->channel_id;
		$id = 3;
		$shop_data = $this->model->getBusShopByChannelId($id);		
		foreach ($shop_data as $v){
			$shop_id[] = $v['id'];
			$shop_name[$v['id']] = $v['business_name'];
		}
		$where[] = 'status=3';
		$where[] = 'shop_id in('.implode(',',$shop_id).')';
		$datas = $this->model->getTicketList($where);
		foreach ($datas as $k=>$v){
			$datas[$k]['buiness_name'] = $shop_name[$v['shop_id']];
		}		
//		print_r('<pre>');
//		print_r($datas);
//		print_r('</pre>');
		$this->tmp->assign('datas',$datas);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","ticket_list.html");
		$this->dp('home/com_center/index');
	}
	//商户列表
	function bus_shop_list()
	{
		
		$id = $this->channel_id;
        $where[] = 'channel_id = '.$id;
        $get = $this->GVar->fget;
        if ($get['status']) {
        	$where[] = 'status = '.$get['status'];
        }
       
        
		$applist = $this->model->getBusShopByChannelId($where);
		
		
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","bus_shop_list.html");
		
		//把区块发送到前
		$this->dp('home/com_center/index');
	}
	//添加商户
	function bus_shop_add()
	{
        $area_list = $this->model->getProvinceList();
        $this->tmp->assign("area_list",$area_list);
		$this->tmp->assign("main_show_block","bus_shop_form.html");
		
		//把区块发送到前
		$this->dp('home/com_center/index');
	}
    //编辑商户
    function bus_shop_edit()
    {
        $id = $this->GVar->fget['id'];
        $applist = $this->model->getBusShopById($id);

        $province_data['parentid']=$applist['province'];
        $applist_city=$this->model->getProvinceList($province_data);
        $this->tmp->assign("applist_city",$applist_city);

        $city_data['parentid']=$applist['city'];
        $applist_country=$this->model->getProvinceList($city_data);
        $this->tmp->assign("applist_country",$applist_country);

        $area_list = $this->model->getProvinceList();
        $this->tmp->assign("area_list",$area_list);

        $this->tmp->assign('applist',$applist);
        $this->tmp->assign("main_show_block","bus_shop_form.html");
       
        //把区块发送到前
        $this->dp('home/com_center/index');

    }
    //商户详细信息
    function bus_shop_detailed()
    {
        $id = $this->GVar->fget['id'];
        $applist = $this->model->getBusShopById($id);
        $this->tmp->assign('applist',$applist);
        $this->tmp->assign("main_show_block","bus_shop_detailed.html");
        
        //把区块发送到前
        $this->dp('home/com_center/index');
    }
    //保存商户信息
    function bus_shop_save()
    {
        $post=$this->GVar->fpost;
        $id = $this->GVar->fget['id'];

        include _SITE_INCLUDE_CLASS_PATH."upfile.php";
        $upfile=new upload();
        
        //企业公章图片
        if ($_FILES['official_seal']){
            $photo=$upfile->upimg($_FILES['official_seal']);
            if ($photo) {
                $post['official_seal']=$photo['photo_dir'].$photo['photo'];
            }
        }
       
        //电子签章制作委托书
        if ($_FILES['signature']){
            $photo=$upfile->upimg($_FILES['signature']);
            if ($photo) {
                $post['signature']=$photo['photo_dir'].$photo['photo'];
            }
        }
        //电子发票服务协议
        if ($_FILES['agreement']){
            $photo=$upfile->upimg($_FILES['agreement']);
            if ($photo) {
                $post['agreement']=$photo['photo_dir'].$photo['photo'];
            }
        }
        $post['c_time'] = time();
        $post['channel_id'] = $this->channel_id;
        $post['status'] = 2;
        $res = $this->model->saveBusiness($post,$id);
        if($res)
        {
            $url = "index.php?o=center&t=bus_shop_list";
            MessageClass::ShowMessageComponent('添加成功',$url,1,_lang_del_fail,null,2);
        }else{
        	 $url = "index.php?o=center&t=bus_shop_list";
            MessageClass::ShowMessageComponent('添加成功',$url,0,_lang_del_fail,null,2);
        }
    }
	//应用列表
	function channel_app_list()
	{
		$id = $this->channel_id;
		$get = $this->GVar->fget;
		
		$applist = $this->model->getChannelApp($id);
		foreach($applist as $k=>$v)
		{
			$bus_name = $this->model->getBusinessName($v['author_business_id']);
			$str = '';
			foreach($bus_name as $key=>$value)
			{
			    $str .=$value['business_name'].',';
			}
			$str = substr($str,0,-1);
			$applist[$k]['business_name']=$str;
		}
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign('id',$id);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu('home'));
		$this->tmp->assign("main_show_block","channel_app_list.html");
		
		//把区块发送到前
		$this->dp('home/com_center/index');
	}
    //添加应用
    function channel_app_add()
    {
    	$this->tmp->assign("main_show_block","channel_app_form.html");
        $this->createMenu();
        //把区块发送到前
        $this->dp('home/com_center/index');
    }
    //添加应用
    function channel_app_save()
    {
        $post=$this->GVar->fpost;
        $id = $this->GVar->fget['id'];
        $post['c_time'] = time();
        $post['author_business_id']=implode(',',$post['author_business_id']);
        $post['channel_id'] = $this->channel_id;
        $res = $this->model->saveChannelApp($post,$id);
        if($res)
        {
            $url = "index.php?o=center&t=channel_app_list";
            MessageClass::ShowMessageComponent('添加成功',$url,1,_lang_del_fail,null,2);
        }
    }
    //编辑应用
    function channel_app_edit()
    {
        $id = $this->GVar->fget['id'];
        $app_list = $this->model->getChannelAppById($id);
        $app_data = explode(',',$app_list['author_business_id']);
        $binding_use = $this->model->binding_use($app_list['channel_id']);
        $this->tmp->assign('binding_use',$binding_use);
        $this->tmp->assign('applist',$app_list);
        $this->tmp->assign('app_data',$app_data);
        $this->tmp->assign("main_show_block","channel_app_form.html");
       
        //把区块发送到前
        $this->dp('home/com_center/index');
    }
    //获取地区列表
    function getAreaList()
    {
        $post=$this->GVar->fpost;
        $applist = $this->model->getProvinceList($post);
        echo json_encode($applist);
    }
    //体验开票
    function testOpenTicket(){
    	
    	$this->tmp->assign("main_show_block","test_open_ticket.html");
		$this->dp('home/com_center/index');
    }
    //ajax创建发票
    function ajaxCreateTicket(){
    	$post = $this->GVar->fpost;
		$goods = explode(',',$post['data']);
		foreach ($goods as $v){
			$price += 0.01;
		}
		
		$number = date('YmdHis').rand(1000,9999);
		$data = array(
			'price'=>$price,
			'shop_id'=>7,
			'c_time'=>time(),
			'number'=>$number,
			'create_user'=>$this->GVar->session['user_info']['id'],
			'status'=>1,
		);
		$res = $this->model->createTicket($data);
		
		
		if ($res) {
			
			foreach ($goods as $k=>$v){
				$insert_data[] = array(
					'wareName'=>$v,
					'count'=>1,
					'unit'=>'',
					'unitPrice'=>0.01,
					'taxRate'=>0,
					'ticket_id'=>$res,
					'c_time'=>time(),
				
				);
				
			}
			$res = $this->model->insertTicketGoods($insert_data);
			echo trim($number);
		}else{
			echo 0;
		}
    }
    //账号信息
    function userInfo(){
    	//获取信息
    	$data = $this->model->getUserInfo($this->GVar->session['user_info']['id']);
    	//    	$data['']
		$data['year'] = date('Y',$data['birthday']);
		$data['month'] = date('m',$data['birthday']);
		$data['day'] = date('d',$data['birthday']);
		$this->tmp->assign('data',$data);
    	$this->tmp->assign("main_show_block","user_form.html");
		$this->dp('home/com_center/index');
    	
    }
    //修改用户信息
    function ajaxChangeUser(){
    	$post = $this->GVar->fpost;
    	$where[] = 'id='.$this->GVar->session['user_info']['id'];
    	$res = $this->model->saveUser($post,$where);
    	
    	echo $res;
    		
    }
    //修改头像 
    function changeHeaderImg(){
    	
    	if($_FILES['header_img']){
	    	include _SITE_INCLUDE_CLASS_PATH."upfile.php";
       		$upfile=new upload();
		    $file = $upfile->upimg($_FILES['header_img']);
		    $data['header_img'] = $file['photo_dir'].$file['photo'];
		  
		    $where[] = 'id='.$this->GVar->session['user_info']['id'];
		    $res = $this->model->saveUser($data,$where);
		    if ($res) {
		    	$this->GVar->session['user_info']['header_img']=$data['header_img'];
				$this->GVar->SetSessionData();
		    }
		    echo $res;
    	}else{
    		echo 0;
    	}
      
    }
    //修改密码
    function changePassword(){
    	$this->tmp->assign("main_show_block","password_form.html");
		$this->dp('home/com_center/index');
    }
    //检查密码
    function checkPassword(){
    	$res = $this->model->checkPassword($this->GVar->fpost['pwd'],$this->GVar->session['user_info']['id']);
    	if ($res) {
    		echo 1;
    	}else{
    		echo 0;
    	}
    	//echo $res;
    	
    }
    //修改密码
    function savePassword(){
    	$res = $this->model->savePassword($this->GVar->fpost['pwd'],$this->GVar->session['user_info']['id']);
    	if ($res) {
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
}

?>